import requests
import re
from bs4 import BeautifulSoup
import traceback


def gethutmltext(url):
    try:
        r = requests.get(url, timeout=30)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return ""


def getstocklist(slist, stockurl):
    html = gethutmltext(stockurl)
    soup = BeautifulSoup(html, 'html.parser')
    a = soup.find_all('a')
    for i in a:
        try:
            href = i.attrs['href']
            slist.append(re.findall(r"[s][hz]\d{6}", href)[0])
        except:
            continue



def getstockinfo(lst, stockurl, fpath):
    count = 0
    for stock in lst:
        url = stockurl + stock + ".html"
        html = gethutmltext(url)
        try:
            if html == "":
                continue
            infodict = {}
            soup = BeautifulSoup(html, 'html.parser')
            stockinfo = soup.find('div', attrs={'class': 'stock-bets'})

            name = stockinfo.find_all(attrs={'class': 'bets-name'})[0]
            infodict.update({'股票信息': name.text.split()[0]})

            keylist = stockinfo.find_all('dt')
            valuelist = stockinfo.find_all('dd')
            for i in range(len(keylist)):
                key = keylist[i].text
                val = valuelist[i].text
                infodict[key] = val
            with open(fpath, 'a+', encoding='utf-8') as f:
                f.write(str(infodict) + '\n')
                count = count+1
                print('\r当前速度：{:.2f}%' .format(count*100/len(lst)), end="")
        except:
            count = count + 1
            print('\r当前速度：{:.2f}% '.format(count * 100 / len(lst)), end="")
            continue


def main():
    stock_list_url = "http://quote.eastmoney.com/stocklist.html"
    stock_info_url = "http://gupiao.baidu.com/stock/"
    output_file = "F:/gupiao.txt"
    slist = []
    getstocklist(slist, stock_list_url)
    getstockinfo(slist, stock_info_url, output_file)


main()